//
//  SettingScene.m
//  lianliankan
//
//

#import "SettingScene.h"
#import "DB.h"

@implementation SettingScene

// 初始化场景
- (id) init
{
	if((self = [super init]))
	{
		// 屏幕尺寸 
		CGSize screenSize = [[CCDirector sharedDirector] winSize];
		
		
		// 背景图片
		CCSprite *bg = [CCSprite spriteWithFile:@"background.png"];
		bg.position = ccp(screenSize.width / 2, screenSize.height / 2);
		[self addChild:bg z:-1 tag:SettingSceneLayerTagBackground];
		
		// 标题
		CCLabelTTF *sceneTitle = [CCLabelTTF labelWithString:@"游戏设置" fontName:@"Marker Felt" fontSize:32];
		sceneTitle.position = ccp(screenSize.width / 2 , screenSize.height - 80);
		[self addChild:sceneTitle z:0 tag:SettingSceneLayerTagTitle];
		
		// 音乐、音效开关
		CCLabelTTF *bgMusicLabel = [CCLabelTTF labelWithString:@"音乐" fontName:@"Marker Felt" fontSize:27];
		bgMusicLabel.position = ccp(screenSize.width / 2 - 40, screenSize.height / 2 + 40);
		[self addChild:bgMusicLabel];
		
		CCLabelTTF *effectLabel = [CCLabelTTF labelWithString:@"音效" fontName:@"Marker Felt" fontSize:27];
		effectLabel.position = ccp(screenSize.width / 2 - 40, screenSize.height / 2 - 40);
		[self addChild:effectLabel];
		
		bgMusicToggleOn = [CCMenuItemImage itemFromNormalImage:@"bgmusicon.png" selectedImage:@"bgmusicoff.png"];
		bgMusicToggleOff = [CCMenuItemImage itemFromNormalImage:@"bgmusicoff.png" selectedImage:@"bgmusicon.png"];	
        
		CCMenuItemToggle *bgMusicToggleMenu;
		// 背景音乐开启
		if ([[DB fetchUser] music]) 
		{
			bgMusicToggleMenu = [CCMenuItemToggle 
                                 itemWithTarget:self 
                                 selector:@selector(toggleBackgroundMusic:) 
                                 items:bgMusicToggleOn, bgMusicToggleOff, nil];
		}
		// 背景音乐关闭
		else
		{
			bgMusicToggleMenu = [CCMenuItemToggle 
                                 itemWithTarget:self 
                                 selector:@selector(toggleBackgroundMusic:) 
                                 items:bgMusicToggleOff, bgMusicToggleOn, nil];
		}
		
		bgMusicToggleMenu.position = ccp(40, 40);
		
        
		effectMusicToggleOn = [CCMenuItemImage itemFromNormalImage:@"effectmusicon.png" selectedImage:@"effectmusicoff.png"];
		effectMusicToggleOff = [CCMenuItemImage itemFromNormalImage:@"effectmusicoff.png" selectedImage:@"effectmusicon.png"];
        
		CCMenuItemToggle *effectMusicToggleMenu;
		// 音效开启
		if ([[DB fetchUser] efx])
		{
			effectMusicToggleMenu = [CCMenuItemToggle 
                                     itemWithTarget:self 
                                     selector:@selector(toggleEffectMusic:) 
                                     items:effectMusicToggleOn, effectMusicToggleOff, nil];
		}
		// 音效关闭
		else
		{
			effectMusicToggleMenu = [CCMenuItemToggle 
									 itemWithTarget:self 
									 selector:@selector(toggleEffectMusic:) 
									 items:effectMusicToggleOff, effectMusicToggleOn, nil];
		}
		effectMusicToggleMenu.position = ccp(40, -40);
		
		
		CCMenu *MusicMenu = [CCMenu menuWithItems:bgMusicToggleMenu, effectMusicToggleMenu, nil];
		
		[self addChild:MusicMenu];
		
		
		// 返回按钮
		CCMenuItem *backMenuItem = [CCMenuItemImage 
									itemFromNormalImage:@"back.png" 
									selectedImage:@"backselected.png" 
									target:self 
									selector:@selector(goBack)];
		
		CCMenu *backMenu = [CCMenu menuWithItems:backMenuItem, nil];
		backMenu.position = ccp(30, 25);
		
		[self addChild:backMenu z:1 tag:SettingSceneLayerTagBackButton];
		
	}
	return self;
}


// 返回
- (void) goBack
{
	[[CCDirector sharedDirector] popScene];
}

// 背景音乐设置
- (void) toggleBackgroundMusic: (id) sender
{
	CCMenuItemToggle *toggleItem = (CCMenuItemToggle *) sender;
	
	User *user = [DB fetchUser];
	
	// 背景音乐开
	if(toggleItem.selectedItem == bgMusicToggleOn)
	{
		[CDAudioManager sharedManager].backgroundMusic.volume = 1.0f;
		user.music = YES;
	}
	// 背景音乐关
	else
	{
		[CDAudioManager sharedManager].backgroundMusic.volume = 0;
		user.music = NO;
	}
    [DB updateUser:user];
}

// 音效设置
- (void) toggleEffectMusic: (id) sender
{
	CCMenuItemToggle *toggleItem = (CCMenuItemToggle *) sender;
	
	User *user = [DB fetchUser];
	
	// 音效开
	if(toggleItem.selectedItem == effectMusicToggleOn)
	{
		user.efx = YES;
	}
	// 音效关
	else
	{
		user.efx = NO;
	}
	[DB updateUser:user];
}

// 释放资源
- (void) dealloc
{
	CCLOG(@"%@:%@", NSStringFromSelector(_cmd), self);
	[super dealloc];
}

@end
